﻿// See https://aka.ms/new-console-template for more information
using NewLife;
using NewLife.Log;
using NewLife.Net;
using NewLife.Net.Handlers;
using NewLife.Threading;

internal class Program
{
    private static void Main(String[] args)
    {
        // 启用控制台日志，拦截所有异常
        XTrace.UseConsole();

        try
        {
            TestServer();
        }
        catch (Exception ex)
        {
            XTrace.WriteException(ex);
        }

        Console.WriteLine("日志集中存储服务器已启动");
        Console.ReadKey();
    }

    private static TimerX _timer;
    private static NewLife.Net.NetServer _server;

    private static void TestServer()
    {
        // 实例化服务端，指定端口，同时在Tcp/Udp/IPv4/IPv6上监听
        var svr = new NetServer.FileServer
        {
            Port = 1227,
            Log = XTrace.Log,
            SessionLog = XTrace.Log,
            StatPeriod = 30,
            Tracer = new DefaultTracer { Period = 15, Log = XTrace.Log },
#if DEBUG
//            SocketLog = XTrace.Log,
//            LogSend = true,
//            LogReceive = true,
//            ProtocolType = NetType.Tcp
#endif
        };

        //svr.Add<StandardCodec>();
        //svr.Add<JsonCodec>();
        svr.Start();

        _server = svr;

        // 定时显示性能数据
        _timer = new TimerX(ShowStat, svr, 100, 1000) { Async = true };
    }

    private static void ShowStat(Object state)
    {
        var msg = "";
        if (state is NewLife.Net.NetServer ns)
            msg = ns.GetStat();
        //else if (state is ISocketRemote ss)
        //    msg = ss.GetStat();

        if (!msg.IsNullOrEmpty()) Console.Title = msg;
    }
}